import { DeleteOutlined } from '@vicons/antd';
import { CreateOutline } from '@vicons/ionicons5';
import { isArray, isFunction } from 'lodash-es';

import type { ActionButton } from '@/components/table/src/types/action-button';
import type { CrudContext } from '@/components/crud/src/context/use-crud-context';

export function useDataActionButtons(context: CrudContext) {
  const modifyButton = shallowReactive<ActionButton>({
    name: '修改',
    icon: CreateOutline,
    key: 'modify',
    disabled: false,
    display: true,
    loading: false,
    permission: 'modify',
    onClick: async rowData => {
      context.handleModify(rowData ?? {});
    },
  });

  const deleteButton = shallowReactive<ActionButton>({
    name: '删除',
    icon: DeleteOutlined,
    key: 'modify',
    disabled: false,
    display: true,
    loading: false,
    permission: 'modify',
    confirmation: true,
    confirmationTip: '确认删除吗？',
    onClick: async rowData => {
      context.handleDelete(rowData ?? {});
    },
  });

  return computed(() => {
    if (isArray(context.dataActionButtons)) {
      return [modifyButton, deleteButton, ...context.dataActionButtons];
    }

    if (isFunction(context.dataActionButtons)) {
      return context.dataActionButtons.call(undefined, {
        modifyButton,
        deleteButton,
      });
    }

    return [modifyButton, deleteButton];
  });
}
